<?php
/**
 * PHPinChains Framework
 *
 * LICENSE
 *
 * This source file is subject to the PHPinChains BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://phpinchains.synflag.com/en/pub/license.php
 *
 * @category   PHPinChains
 * @package    phpinchains
 * @copyright  Copyright (c) 2005-2007 SynFlag Online Agentur - Germany
 * @license    http://phpinchains.synflag.com/en/pub/license.php PHPinChains BSD License
 * @since Date 26.01.2006
 */


m2import("Log.php");
m2import('phpinchains/psp/OutputWriter.php');

/**
 * Context object with the output writer and other generally used objects
 *
 * @category   PHPinChains
 * @package    phpinchains
 * @copyright  Copyright (c) 2005-2007 SynFlag Online Agentur - Germany
 * @license    http://phpinchains.synflag.com/en/pub/license.php PHPinChains BSD License
 */
class phpinchains_ApplicationContext extends phpinchains_RuntimeConfig {

    /**
     * Output writer
     * @access public
     * @var phpinchains_psp_OutputWriter
     */
    var $out;
    /**
     * JavaScript Output writer
     * @access public
     * @var phpinchains_psp_OutputWriter
     */
    var $jsout;

    /**
     * Singleton method
     *
     * @static
     * @access public
     * @return phpinchains_ApplicationContext Context instance
     */
    static function getInstance() {
        static $obj = null;
        if ($obj == null) {
            $obj = new phpinchains_ApplicationContext();
            // to reduce coupling with global variables
            $obj->setRef('env', env::getInstance());
            $obj->out    = new phpinchains_psp_OutputWriter();
            $obj->jsout  = new phpinchains_psp_OutputWriter();
            $obj->log    = &Log::factory('file', MODEL2_LOG_DIR.'out.log', 'LOG');
        }
        return $obj;
    }

    /**
     * Get the output writer
     *
     * PSP output writer which contains the complete output of the Action Dispatcher based app.
     *
     * @return phpinchains_psp_OutputWriter The output writer
     * @access public
     */
    function & getOutputWriter() {
        return $this->out;
    }

    /**
     * Write a string to the log
     * @return Log The log instance
     * @access public
     */
    function log($str) {
        return $this->log->log($str);
    }

    function debug($str) {
        if ((isset($GLOBALS['cfgDebugModeToScreen']) && $GLOBALS['cfgDebugModeToScreen']))
            print "$str<br/>";
        if ((isset($GLOBALS['cfgDebugMode']) && $GLOBALS['cfgDebugMode']) || MODEL2_DEBUG_MODE)
            return $this->log->debug($str);
    }
    function info($str) {
        return $this->log->info($str);
    }
    function notice($str) {
        return $this->log->notice($str);
    }
    function warning($str) {
        return $this->log->warning($str);
    }
    function err($str) {
        return $this->log->err($str);
    }
    function crit($str) {
        return $this->log->crit($str);
    }
    function alert($str) {
        return $this->log->alert($str);
    }
    function emerg($str) {
        return $this->log->emerg($str);
    }

    /**
     * Get the javascript output writer for writing js in the head
     * @return phpinchains_psp_OutputWriter The output writer
     * @access public
     */
    function & getJSOutputWriter() {
        return $this->jsout;
    }

    function setModuleBaseDir ($mbdir) {
        $this->moduleBaseDir = $mbdir;
    }

    function getModuleBaseDir () {
        return $this->moduleBaseDir;
    }

    function & getEnv () {
        return $this->data['env'];
    }

}

?>
